// 虚拟滚动指令
import PerfectScrollbar from 'perfect-scrollbar'
import 'perfect-scrollbar/css/perfect-scrollbar.css'

const elScrollBar = el => {
  if (el.__ps__ instanceof PerfectScrollbar) {
    el.__ps__.update()
  } else {
    el.__ps__ = new PerfectScrollbar(el)
  }
}

export function setupVirtualScrollDirective(app) {
  app.directive('virtual-scroll', {
    mounted(el) {
      const rules = ['fixed', 'absolute', 'relative', 'sticky']
      if (!rules.includes(window.getComputedStyle(el, null).position)) {
        console.error("element missing attributes: position ('fixed', 'absolute', 'relative', 'sticky')")
      }
      elScrollBar(el)
    }
  })
}
